<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Category as CategoryModel;

class Category extends Controller
{

    public function lst()
    {
        /*
         * $cates = db('category')->select();
         * $this->assign('list', $cates);
         */
        $cate = new CategoryModel();
        $list = $cate->catetree();
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function add()
    {
        $category = new CategoryModel();
        if (request()->isPost()) {
            $data = [
                'cate_name' => input('cate_name'),
                'cate_ename' => input('cate_ename'),
                'cate_keywords' => input('cate_keywords'),
                'cate_desc' => input('cate_desc'),
                'cate_type' => input('cate_type'),
                'parentid' => input('parentid'),
                'cate_content' => input('cate_content')
            ];
            if ($_FILES['cate_pic']['tmp_name']) {
                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('cate_pic');
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                if ($info) {
                    $data['cate_pic'] = '/uploads/' . $info->getSaveName();
                } else {
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            } else {
                $data['cate_pic'] = '';
            }
            if ($category->insert($data)) {
                $this->success('添加成功', 'lst');
            } else {
                $this->error('添加失败');
            }
            return;
        }
        $catetree = $category->catetree();
        $this->assign('catetree', $catetree);
        return $this->fetch();
    }

    public function edit()
    {
        $cate_id = input('cate_id');
        $cate = new CategoryModel();
        $cateresa = $cate->find($cate_id);
        $pic = $cateresa['cate_pic'];
        if (request()->isPost()) {
            $data = [
                'cate_name' => input('cate_name'),
                'cate_ename' => input('cate_ename'),
                'cate_keywords' => input('cate_keywords'),
                'cate_desc' => input('cate_desc'),
                'cate_type' => input('cate_type'),
                'parentid' => input('parentid'),
                'cate_content' => input('cate_content')
            ];
            if ($_FILES['cate_pic']['tmp_name'] != '') {
                $file = request()->file('cate_pic');
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                if ($info) {
                    // 成功上传后 获取上传信息
                    // dump($info);die();
                    $data['cate_pic'] = '/uploads/' . $info->getSaveName();
                    if ($pic != '') {
                        if (is_dir($pic)) {
                            unlink('public/static' . $pic);
                        }
                    }
                } else {
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            } else {
                if (is_dir($pic)) {
                    $data['cate_pic'] = $pic;
                } else {
                    $data['cate_pic'] = '';
                }
            }
            
            if ($cate->where($cate_id)->update($data)) {
                $this->success('栏目修改成功', 'lst');
            } else {
                $this->error('栏目修改失败！');
            }
            
            return;
        }
        $this->assign('cateresa', $cateresa);
        $cateres = $cate->catetree();
        $this->assign('cateres', $cateres);
        return $this->fetch();
    }

    public function del($id)
    {
        $del = db('category')->delete($id);
        if ($del == true) {
            $this->success('删除成功', 'lst');
        } else {
            $this->error('删除失败');
        }
    }

    public function bdel()
    {
        $bdel = input('bdel/a');
        if ($bdel) {
            $delid = implode(',', $bdel);
            $cates = db('category');
            if ($cates->delete($delid)) {
                $this->success('批量删除栏目成功！', 'lst');
            } else {
                $this->error('批量删除栏目失败！');
            }
        }
    }
}